package com.ccepc.algmanage.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccepc.algmanage.service.AlgorithmConfigService;
import com.ccepc.algmanage.utils.ApiRequestUtils;
import com.ccepc.common.core.util.R;
import com.ccepc.common.log.annotation.SysLog;
import com.ccepc.demo.api.dto.AlgorithmConfigDto;
import com.ccepc.demo.api.entity.AlgorithmConfig;
import com.ccepc.demo.api.entity.ApiData;
import com.ccepc.demo.api.entity.DeployInfo;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 *
 *
 * @author pigx code generator
 * @date 2023-11-15 09:12:19
 */
@RestController
@RequestMapping("/algorithmconfig" )
@Api(value = "algorithmconfig", tags = "算法配置")
public class AlgorithmConfigController {

    @Resource
    private AlgorithmConfigService algorithmConfigService;

    @ApiOperation(value = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('algmanage_algorithmconfig_view')" )
    public R getAlgorithmConfigPage(Page<AlgorithmConfig> page, AlgorithmConfig algorithmConfig) {
        return R.ok(algorithmConfigService.page(page, Wrappers.query(algorithmConfig)));
    }

    @ApiOperation(value = "通过id查询")
    @GetMapping("/{deployId}" )
    @PreAuthorize("@pms.hasPermission('algmanage_algorithmconfig_view')" )
    public R getById(@PathVariable("deployId" ) Integer deployId) {
        return R.ok(algorithmConfigService.getByDeployId(deployId));
    }

    @ApiOperation(value = "新增")
    @SysLog("新增" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('algmanage_algorithmconfig_add')" )
    public R saveOrUpdateDto(@RequestBody AlgorithmConfigDto algorithmConfigDto) {
        return R.ok(algorithmConfigService.saveOrUpdateDto(algorithmConfigDto));
    }

    @ApiOperation(value = "修改")
    @SysLog("修改" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('algmanage_algorithmconfig_edit')" )
    public R updateById(@RequestBody AlgorithmConfig algorithmConfig) {
        return R.ok(algorithmConfigService.updateById(algorithmConfig));
    }

    @ApiOperation(value = "通过id删除")
    @SysLog("通过id删除" )
    @DeleteMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('algmanage_algorithmconfig_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(algorithmConfigService.removeById(id));
    }

    @ApiOperation(value = "通过id查询已经配置了的摄像头编码")
    @SysLog("通过id查询已经配置了的摄像头编码" )
    @GetMapping("/ready/{id}" )
    @PreAuthorize("@pms.hasPermission('algmanage_algorithmconfig_idview')" )
    public R getReadyConfig(@PathVariable("id" ) Integer id) {
        return R.ok(String.join(",", algorithmConfigService.getById(id).getCameraIds()));
    }

    @ApiOperation(value = "新增")
    @SysLog("新增" )
    @PostMapping("/sendConfig")
    @PreAuthorize("@pms.hasPermission('algmanage_algorithmconfig_add')" )
    public ResponseEntity<Object> sendConfig(@RequestBody ApiData apiData) {
        String url = "http://" + apiData.getUrl() + "/setting";
        return ApiRequestUtils.postData(url, apiData.getBody());
    }

}
